package com.zhj.network.udp.chat;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @PackageName:com.zhj.network.udp.chat
 * @ClassName:UdpReceiveDemo01
 * @auter: 朱慧杰
 * @date:2020/7/21 0021 13:47
 **/
public class UdpReceiveDemo01 {

    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(6666);

        while (true) {

            byte[] buffer = new byte[1024];

            DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);

            socket.receive(packet);  // 阻塞式接收

            byte[] data = packet.getData();
            String receiveData = new String(data, 0, data.length);
            System.out.println(receiveData);

            if (receiveData.equals("bye")) {
                break;
            }

        }

        socket.close();
    }

}
